package com.namuan.jp_stock.util;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import com.namuan.jp_stock.object.Account;
import com.namuan.jp_stock.object.ActivityComment;
import com.namuan.jp_stock.object.FullTrackback;
import com.namuan.jp_stock.object.ShotActivity;
import com.namuan.jp_stock.object.FullActivity;
import com.namuan.jp_stock.object.Stock;
import com.namuan.jp_stock.object.Trackback;


public class ObjUtils {

	public static Account jsonToAccount(JSONObject json)
			throws NumberFormatException, JSONException {

		Account account = new Account();
		account.setUsernickname(json.getString("usernickname"));
		account.setPw(json.getString("pw"));
		account.setEmail(json.getString("email"));
		account.setUserscore(json.getInt("userscore"));
		account.setProfilepic(json.getString("profilepic"));
		account.setTwitter(json.getString("twitter"));
		account.setFacebook(json.getString("facebook"));
		account.setSession(json.getString("session"));
		

		return account;
	}

	@SuppressWarnings("unchecked")
	public static ArrayList<Account> jsonToAccountList(JSONArray json)
			throws JSONException {

		ArrayList<Account> result = new ArrayList<Account>(json.length());
		for (int i = 0; i < json.length(); i++) {
			JSONObject object = json.getJSONObject(i);
			Account account = new Account();
			account.setUsernickname(object.getString("usernickname"));
			account.setEmail(object.getString("email"));
			account.setProfilepic(object.getString("profilepic"));
			account.setTwitter(object.getString("twitter"));
			account.setFacebook(object.getString("facebook"));
			result.add(account);
		}

		return result;
	}

	public static Stock jsonToStock(JSONObject json)
			throws NumberFormatException, JSONException {

		Stock stock = new Stock();
		stock.setStocknumber(json.getString("stocknumber"));
		stock.setStockname(json.getString("stockname"));
		stock.setStocktype(json.getString("stocktype"));
		stock.setStockscore(json.getString("stockscore"));
		stock.setStockmayor(json.getString("stockmayor"));

		return stock;
	}
	
	

	public static ArrayList<Stock> jsonToStockList(JSONArray json)
			throws JSONException {

		ArrayList<Stock> result = new ArrayList<Stock>(json.length());
		for (int i = 0; i < json.length(); i++) {
			JSONObject object = json.getJSONObject(i);
			Stock stock = new Stock();
			
			stock.setStocknumber(object.getString("stocknumber"));
			stock.setStockname(object.getString("stockname"));
			stock.setStocktype(object.getString("stocktype"));
			stock.setStockscore(object.getString("stockscore"));
			stock.setStockmayor(object.getString("stockmayor"));			
			
			result.add(stock);
		}

		return result;
	}

	
	public static ShotActivity jsonToShotActivity(JSONObject json)
			throws NumberFormatException, JSONException {
		
		ShotActivity activity = new ShotActivity();
		activity.setActivityid(json.getString("activityid"));
		activity.setUsernickname(json.getString("usernickname"));
		activity.setProfilepic(json.getString("profilepic"));
		
		activity.setStocknumber(json.getString(("stocknumber")));
		activity.setStockname(json.getString(("stockname")));
		activity.setStocktype(json.getString(("stocktype")));
		
		activity.setComment(json.getString("comment"));
		activity.setTimestamp(json.getString("timestamp"));
		String ismember = json.getString("ismemberactivity");
		if(ismember.equals("1")){
			activity.setMember(true);
		} else {
			activity.setMember(false);
		}
		
		activity.setRecommendation(json.getString("recommendation"));
		activity.setTrackbackcount(json.getString("trackbackcount"));

		return activity;
	}
	/*
	public static ArrayList<String> jsonToActivityPicsList(JSONArray json)
	throws JSONException {
		ArrayList<String> result = new ArrayList<String>(
				json.length());
		for (int i = 0; i < json.length(); i++) {
			JSONObject object = json.getJSONObject(i);
			String winePic = new String();
			winePic = object.getString("winepic");			
			result.add(winePic);
		}
		return result;		
	}*/
	public static ArrayList<ShotActivity> jsonToShotActivityList(JSONArray json)
			throws JSONException {

		ArrayList<ShotActivity> result = new ArrayList<ShotActivity>(
				json.length());
		for (int i = 0; i < json.length(); i++) {
			JSONObject object = json.getJSONObject(i);
			ShotActivity activity = new ShotActivity();
			activity.setActivityid(object.getString("activityid"));
			activity.setUsernickname(object.getString("usernickname"));
			activity.setProfilepic(object.getString("profilepic"));
			
			activity.setStocknumber(object.getString(("stocknumber")));
			activity.setStockname(object.getString(("stockname")));
			activity.setStocktype(object.getString("stocktype"));
			
			activity.setComment(object.getString("comment"));
			activity.setTimestamp(object.getString("timestamp"));
			String ismember = object.getString("ismemberactivity");
			if(ismember.equals("1")){
				activity.setMember(true);
			} else {
				activity.setMember(false);
			}
			activity.setRecommendation(object.getString("recommendation"));
			activity.setTrackbackcount(object.getString("trackbackcount"));
			
			
			result.add(activity);
		}

		return result;
	}
	

	public static FullActivity jsonToFullActivity(JSONObject json)
			throws NumberFormatException, JSONException {

		FullActivity fullActivity = new FullActivity();
		fullActivity.setActivityid(json.getString("activityid"));
		fullActivity.setUsernickname(json.getString("usernickname"));
		fullActivity.setProfilepic(json.getString("profilepic"));

		fullActivity.setTrackbackcount(json.getString(("trackbackcount")));
		fullActivity.setRecommendation(json.getString(("recommendation")));
		
		fullActivity.setStocknumber(json.getString(("stocknumber")));
		fullActivity.setStockname(json.getString(("stockname")));
		
		fullActivity.setStocktype(json.getString("stocktype"));
		
		fullActivity.setComment(json.getString("comment"));
		fullActivity.setTimestamp(json.getString("timestamp"));
		String ismember = json.getString("ismemberactivity");
		if(ismember.equals("1")){
			fullActivity.setMember(true);
		} else {
			fullActivity.setMember(false);
		}
		
		fullActivity.setEmail(json.getString("email"));
		fullActivity.setTwitter(json.getString(("twitter")));
		fullActivity.setFacebook(json.getString(("facebook")));
		
		

		return fullActivity;
	}
	
	public static Trackback jsonToTrackback(JSONObject json)
			throws NumberFormatException, JSONException {

		Trackback trackback = new Trackback();
		int trackbackid = Integer.parseInt(json.getString("trackbackid"));
		trackback.setActivityid(trackbackid);
		trackback.setActivityid(json.getInt("activityid"));
		trackback.setUsernickname(json.getString("usernickname"));
		trackback.setProfilepic(json.getString("profilepic"));

		trackback.setComment(json.getString("comment"));
		int recommendation = Integer.parseInt(json.getString("recommendation"));
		trackback.setRecommendation(recommendation);
		trackback.setTimestamp(json.getString("timestamp"));
		trackback.setTrackbackpw(json.getBoolean("activitypw"));

		return trackback;
	}
	
	public static ArrayList<Trackback> jsonToTrackbackList(JSONArray json)
			throws JSONException {
		ArrayList<Trackback> result = new ArrayList<Trackback>(json.length());
		for (int i = 0; i < json.length(); i++) {
			JSONObject object = json.getJSONObject(i);
			Trackback trackback = new Trackback();
			int trackbackid = Integer.parseInt(object.getString("trackbackid"));
			trackback.setTrackbackid(object.getInt("trackbackid"));
			trackback.setActivityid(object.getInt("activityid"));
			trackback.setUsernickname(object.getString("usernickname"));
			trackback.setProfilepic(object.getString("profilepic"));

			trackback.setComment(object.getString("comment"));
			int recommendation = Integer.parseInt(object.getString("recommendation"));
			trackback.setRecommendation(recommendation);
			trackback.setTimestamp(object.getString("timestamp"));
			trackback.setTrackbackpw(object.getBoolean("activitypw"));

			result.add(trackback);
		}
		return result;
	}
	


	public static FullTrackback jsonToFullTrackback(JSONObject json)
			throws NumberFormatException, JSONException {

		FullTrackback fullTrackback = new FullTrackback();
		fullTrackback.setTrackbackid(json.getInt("trackbackid"));
		fullTrackback.setActivityid(json.getInt("activityid"));
		fullTrackback.setUsernickname(json.getString("usernickname"));
		fullTrackback.setProfilepic(json.getString("profilepic"));
		
		fullTrackback.setComment(json.getString("comment"));
		fullTrackback.setTimestamp(json.getString("timestamp"));
		fullTrackback.setTrackbackpw(json.getBoolean("trackbackpw"));
		
		fullTrackback.setEmail(json.getString("email"));
		fullTrackback.setTwitter(json.getString(("twitter")));
		fullTrackback.setFacebook(json.getString(("facebook")));

		return fullTrackback;
	}

	public static ArrayList<ActivityComment> jsonToActivityComment(JSONArray json) throws JSONException {
		ArrayList<ActivityComment> result = new ArrayList<ActivityComment>(
				json.length());
		for (int i = 0; i < json.length(); i++) {
			JSONObject object = json.getJSONObject(i);
			ActivityComment activityComment = new ActivityComment();
			
			activityComment.setUserid(object.getString("usernickname"));
			activityComment.setActivityid(object.getString("activityid"));
			activityComment.setProfilepic(object.getString("profilepic"));
			activityComment.setTimestamp(object.getString("timestamp"));
			activityComment.setComment(object.getString("comment"));
			
			result.add(activityComment);
		}

		return result;
	}
	

}
